//
//  NSTimer+SHBlockSupport.m
//  SDemo
//
//  Created by gg on 2018/10/30.
//  Copyright © 2018 gg. All rights reserved.
//

#import "NSTimer+SHBlockSupport.h"

@implementation NSTimer (SHBlockSupport)

+(NSTimer *)sh_scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats handle:(SHBlock)handle {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval target:self
                                                    selector:@selector(onTimerUpdateBlock:)
                                                    userInfo:[handle copy]
                                                     repeats:repeats];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    //判断是不是在主线程上
    if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
        //子线程runloop 要手动开启
        [[NSRunLoop currentRunLoop] run];
    }
    return timer;
}

-(void)onTimerUpdateBlock:(NSTimer *)timer {
    SHBlock block = timer.userInfo;
    if (block) {
        block();
    }
}

@end
